package Arrays101.Test;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * @author Alger
 * @date 2021/6/21  17:24
 */
public class Student extends Person{
    private String name;

    Student(String name, int age) {
        super(name, age);
    }
    Student() {
    }
    public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException, NoSuchMethodException, InvocationTargetException {
        Person p = new Student();
        //获取Class对象
        Class clazz1 =p.getClass();
        Class clazz2 = Person.class;
        Class clazz3=Class.forName("Arrays101.Test.Person");
        //获取类的成员属性信息
        Field[] declaredFields = clazz3.getDeclaredFields();
        for (Field field:declaredFields){
            System.out.println(field);
        }
        //获取类的所有方法信息
        Method[] declaredMethods = clazz3.getDeclaredMethods();
        for (Method method:declaredMethods){
            System.out.println(method);
        }
        //获取类的构造方法信息
        Constructor[] constructors = clazz3.getConstructors();
        for (Constructor c:constructors){
            System.out.println(c);
        }
        Constructor declaredConstructor = clazz3.getDeclaredConstructor(String.class, int.class);
        Person person = (Person) declaredConstructor.newInstance("张杰", 11);
        System.out.println(person.toString());

        Method method = clazz3.getMethod("setName", String.class);
        Constructor constructor = clazz3.getConstructor();
        Person person1 = (Person) constructor.newInstance();
        method.invoke(person1,"张杰");

    }
}
